﻿#region Header

// MS codename Velocity Distributed Cache Extensions.
// (c) 2009

#endregion


#region Usings

using System;
using System.Diagnostics;
using System.Threading;

using Caching.Extensions.Core.Impl;
using Caching.Extensions.Core.Setup;

using NUnit.Framework;

#endregion


namespace Caching.Extension.Core.Tests.Instrumentation
{
    [TestFixture(Description = "Learning performance counters")]
    public class PerformanceCounters : AssertionHelper
    {
        private const string CategoryName = "SmartCacheTest";
        private const string CategoryHelp = "SmartCache performance data";


        [Test(Description = "Create/remove performance counters")]
        public void Category_CreateRemove()
        {
            var category = PerformanceCounterCategory.Create(
                CategoryName, CategoryHelp,
                PerformanceCounterCategoryType.MultiInstance,
                "Named Caches", "# of named caches"
                );

            Expect(category, Is.Not.Null);
        }


        [Test]
        public void LocalCache_Hit()
        {
            LocalCachePerformanceLogger logger = new LocalCachePerformanceLogger();
            Random random = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < 40; i++)
            {
                logger.OnCacheHit();
                Thread.Sleep(random.Next(100, 800));
                if (i % 9 == 1)
                    logger.OnCleanup();
            }

        }

        

        [SetUp]
        public void SetUp()
        {
            if (PerformanceCounterCategory.Exists(CategoryName))
                PerformanceCounterCategory.Delete(CategoryName);
        }


        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            new SmartCacheInstrumentationConfigurator().CreateLocalCacheCategory(true);
        }


        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            // new SmartCacheInstrumentationConfigurator().RemoveLocalCacheCategory();
        }


        [TearDown]
        public void TearDown()
        {
        }
    }
}